<?php

namespace App\Services;

use App\Models\Cms_Store;
use App\Models\Sys_User_Store;
use Endroid\QrCode\Builder\Builder;
use Endroid\QrCode\Encoding\Encoding;
use Endroid\QrCode\ErrorCorrectionLevel\ErrorCorrectionLevelHigh;
use Endroid\QrCode\Label\Alignment\LabelAlignmentCenter;
use Endroid\QrCode\Label\Font\NotoSans;
use Endroid\QrCode\RoundBlockSizeMode\RoundBlockSizeModeMargin;
use Endroid\QrCode\Writer\PngWriter;

class StoreService
{
    public function getAppStoreList($params)
    {
        $storeids = Sys_User_Store::where('userid',$params['uid'])->pluck('storeid');
        $list = Cms_Store::whereIn('id',$storeids)
            ->Nodel()
            ->orderBy('id','desc')
            ->get();
        $arr = [];
        foreach ($list as $key => $val){
            $arr[$key] = ['label' => $val->name,'value' => $val->id];
        }
        return $arr;
    }

    public function getStoreList($params)
    {
        if(isset($params['uid'])){
            $storeids = Sys_User_Store::where('userid',$params['uid'])->pluck('storeid');
            return Cms_Store::getFilter($params)->whereIn('id',$storeids)->select(['id','name'])
            ->Nodel()
            ->orderBy('id','desc')
            ->get();
        }else{
            return Cms_Store::getFilter($params)->select(['id','name'])
            ->Nodel()
            ->orderBy('id','desc')
            ->get();
        }
        
    }

    public function getList($params)
    {
        $result = Cms_Store::getFilter($params)
            ->Nodel()
            ->orderBy('id','desc')
            ->paginate($params['pageSize']);
        $list['list'] = $result->items();
        $list['total'] = $result->total();
        return $list;
    }

    public function storeUpdate($params)
    {
        $res = Cms_Store::updateOrCreate(
            ['id' => $params['id']],
            ['name' => $params['name']],
        );


        $text = env('H5_URL').'?storeid='.$res->id;
        $publicPath = app()->basePath('public');
        $result = Builder::create()
            ->writer(new PngWriter())
            // 二维码文本
            ->data($text)
            // 内容编码
            ->encoding(new Encoding('UTF-8'))
            // 容错等级(越高二维码越密集)
            ->errorCorrectionLevel(new ErrorCorrectionLevelHigh())
            // 二维码内容区域大小
            ->size(300)
            // 二维码内容外边距大小
            ->margin(10)
            ->build();
        $result->saveToFile($publicPath . '/qrcodes/qrcode'.$res->id.'.png');

        $res->code = env('API_URL').'qrcodes/qrcode'.$res->id.'.png';
        $res->save();
        if($res)
            return true;
        else
            return false;
    }

    public function storeDelete($params)
    {
        if(is_string($params['ids']))
            $result = Cms_Store::whereIn('id',explode(',',$params['ids']))->update(['is_del'=>'Y']);
        else
            $result = Cms_Store::where('id',$params['ids'])->update(['is_del'=>'Y']);

        if($result)
            return true;
        else
            return false;
    }
}
